package com.wangjiyuan.giftshuo.ui.fragment.listtop.other;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.base.BaseFragment;
import com.wangjiyuan.giftshuo.ui.fragment.listtop.adapter.ViewPagerAdapter;
import com.wangjiyuan.giftshuo.ui.fragment.listtop.message.DataMessage;
import com.wangjiyuan.giftshuo.ui.fragment.listtop.message.UrlMessage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 *
 * 绑定TabLayout和ViewPager
 *
 * 设置对分享ImageView的监听
 *
 */
public class ListTopFragment extends BaseFragment {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private ImageView img_fm_share;
    private List<Fragment> mFragmentList = new ArrayList<>();
    private ViewPagerAdapter viewPagerAdapter;
    private ListTopItemFragment listTopItemFragment;
    private Map<String, String> params = new HashMap<>();

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_list_top, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        img_fm_share = (ImageView) view.findViewById(R.id.img_fm_share);
        mTabLayout = (TabLayout) view.findViewById(R.id.tab_listTop);
        mViewPager = (ViewPager) view.findViewById(R.id.viewPager_listTop);

    }

    /**
     *
     * @param savedInstanceState
     *
     * 1、通过循环添加Fragment以及传值
     * 2、ViewPager和适配器的绑定
     * 3、ViewPager和TabLayout的绑定
     * 4、设置分享监听
     */
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initFragmentData();
        initFragmentView();
        initTabLayout();

        //设置分享监听
        img_fm_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "share", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initTabLayout() {
        mTabLayout.setupWithViewPager(mViewPager);
    }

    private void initFragmentData() {
        for (int i = 0; i < DataMessage.TAB_NAME.length; i++){
            if (i == 1){
                params.put(UrlMessage.Params.LIMIT, "100");
                params.put(UrlMessage.Params.OFFSET, UrlMessage.DefaultParams.OFFSET);
                listTopItemFragment = new ListTopItemFragment().newInstance((i+1), params);
            }
            params.put(UrlMessage.Params.LIMIT, UrlMessage.DefaultParams.LIMIT);
            params.put(UrlMessage.Params.OFFSET, UrlMessage.DefaultParams.OFFSET);
            listTopItemFragment = new ListTopItemFragment().newInstance((i+1), params);
            mFragmentList.add(listTopItemFragment);
        }
    }

    private void initFragmentView() {
        viewPagerAdapter = new ViewPagerAdapter(
                getActivity().getSupportFragmentManager(), mFragmentList);
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setAdapter(viewPagerAdapter);
    }
}
